package com.ml.springbootidus.redis.impl;

import com.alibaba.fastjson.JSON;
import com.ml.springbootidus.pojo.UserInfo;
import com.ml.springbootidus.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RedisServiceImpl implements RedisService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void setAllUser(List<UserInfo> listUser) {
        redisTemplate.opsForValue().set("thisredis::getuserrs", JSON.toJSONString(listUser));
    }

    @Override
    public List<UserInfo> getAllUser() {
        Object users = redisTemplate.opsForValue().get("thisredis::getuserrs");
        List<UserInfo> userListJson = null;
        if (users != null) {
            userListJson = JSON.parseArray(users.toString(), UserInfo.class);
        }
        return userListJson;
    }
}
